package com.irdstudio.efp.esb.service.impl.hj;

import static com.irdstudio.basic.sequence.service.DefaultPatternDescriptor.DAY;
import static com.irdstudio.basic.sequence.service.DefaultPatternDescriptor.HOUR;
import static com.irdstudio.basic.sequence.service.DefaultPatternDescriptor.MIN;
import static com.irdstudio.basic.sequence.service.DefaultPatternDescriptor.MONTH;
import static com.irdstudio.basic.sequence.service.DefaultPatternDescriptor.SECOND;
import static com.irdstudio.basic.sequence.service.DefaultPatternDescriptor.SEQ;
import static com.irdstudio.basic.sequence.service.DefaultPatternDescriptor.YEAR_2;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.irdstudio.basic.framework.core.constant.ESBConstant;
import com.irdstudio.basic.framework.core.util.SpringContextUtils;
import com.irdstudio.basic.framework.core.util.TraceUtil;
import com.irdstudio.basic.sequence.service.DefaultPatternDescriptor;
import com.irdstudio.basic.sequence.service.PatternDescriptor;
import com.irdstudio.basic.sequence.service.facade.PatternedLimitableSeqService;
import com.irdstudio.efp.esb.common.client.resp.EsbRespSysHeadBean;
import com.irdstudio.efp.esb.common.constant.ESBClientConstance;
import com.irdstudio.efp.esb.common.constant.hj.HjConstant;
import com.irdstudio.efp.esb.service.bo.req.hj.ReqDownLoadFileBean;
import com.irdstudio.efp.esb.service.client.ESBClientFactory;
import com.irdstudio.efp.esb.service.facade.hj.DownLoadFileService;

/**
 * 贷款文件下载通知
 * 
 * @description:TODO
 * @date:2019年1月4日 上午11:26:47
 * @author:cenyj
 */
@Service("downLoadFileService")
public class DownLoadFileServiceImpl implements DownLoadFileService {
	Logger logger = LoggerFactory.getLogger(CustomerInfoCreateServiceImpl.class);

	public boolean DownLoadFileNotice(ReqDownLoadFileBean reqDownLoadFileBean) {
		try {
			//生成ESB系统的全局流水号(GlblSrlNo)
			String glblSrlNo = generateESBTradeSerialNo();
			//设置ESB系统的全局流水号(GlblSrlNo)
			TraceUtil.setTraceId(glblSrlNo);
		} catch (Exception e1) {
			e1.printStackTrace();
			logger.info("生成ESB系统的全局流水号(GlblSrlNo)出错，错误信息：" +e1.getMessage());
			return false;
		}
		if (HjConstant.ChannelNoEnum.CHANNEL_NO_MS.getValue().equals(reqDownLoadFileBean.getPrtnrCd())) {
			// 设置贷款文件下载通知-平台ID
			TraceUtil.setPlafmID(ESBClientConstance.msPlafmID);
			// 设置贷款文件下载通知-机构Id
			TraceUtil.setInstId(ESBClientConstance.msInstId);
		} else if (HjConstant.ChannelNoEnum.CHANNEL_NO_BD.getValue().equals(reqDownLoadFileBean.getPrtnrCd())) {
			// 设置贷款文件下载通知-平台ID
			TraceUtil.setPlafmID(ESBClientConstance.bdPlafmID);
			// 设置贷款文件下载通知-机构Id
			TraceUtil.setInstId(ESBClientConstance.bdInstId);
		}

		boolean rs = true;
		EsbRespSysHeadBean esbRespSysHeadBean = new EsbRespSysHeadBean();
		logger.info("根据文件名：" + reqDownLoadFileBean.getFileNm() + "通知互金");
		try {
			// MBSD_BP_LM-1220-0102 调用esb客户端服务ESBTradeNoConstance.DOWNLOADFILE
			esbRespSysHeadBean = ESBClientFactory.buildClient().withBody(reqDownLoadFileBean).withTradeNo("30220003").withScene("03")
					.create().sendAndReceive().getSysHead();
			String retCd = esbRespSysHeadBean.getRetInfArry().get(0).getRetCd();

			if (ESBConstant.SUCCESS_000000.equals(retCd) == false) {
				logger.info("通知互金出错");
				rs = false;
			}
			// String retMsg = esbRespSysHeadBean.getRetInfArry().get(0).getRetMsg();
		} catch (Exception e) {
			logger.error(e.getMessage());
			logger.info("根据文件名：" + reqDownLoadFileBean.getFileNm() + "通知互金出错");
			rs = false;
		} finally {
			// 清除全局链路跟踪信息
			TraceUtil.clear();
		}
		logger.info("根据文件名：" + reqDownLoadFileBean.getFileNm() + "通知互金结束");
		return rs;
	}
	
	/* 生成ESB系统的全局流水号(GlblSrlNo)规则  */
	private static final PatternDescriptor pattern = 
			DefaultPatternDescriptor.of(ESBClientConstance.CnsmrSysNo + 
					YEAR_2 + MONTH + DAY + HOUR + MIN + SECOND + SEQ, 6);
	
	/* 生成ESB系统的全局流水号(GlblSrlNo) */
	private String generateESBTradeSerialNo() throws Exception {
		PatternedLimitableSeqService sequenceService = SpringContextUtils.getBean(PatternedLimitableSeqService.class);
		return sequenceService.getPatternedSequence("ESB", pattern);
	}
}
